package com.airlines.crewassignment.service.linkline.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.airlines.base.config.datasource.PrimaryDataSourceConfig;
import com.airlines.crewassignment.constant.CrewConstant;
import com.airlines.crewassignment.dao.FlightInfoMapper;
import com.airlines.crewassignment.domain.DateTimeRange;
import com.airlines.crewassignment.domain.FlightInfo;
import com.airlines.crewassignment.entity.linkline.FlightCrewLinkLineNo;
import com.airlines.crewassignment.entity.linkline.TaskLoopFlightFact;
import com.airlines.crewassignment.service.linkline.FlightCrewLinkLineNoService;
import com.airlines.crewassignment.service.linkline.FlightInfoService;
import com.airlines.crewassignment.service.linkline.TaskLoopFlightFactService;

/**
 * @author xxxx
 * @date 2018年6月19日 
 * @description
 */
@Service
public class FlightInfoServiceImpl implements FlightInfoService{
	@Autowired
	private FlightInfoMapper flightInfoMapper;
	@Autowired
	private FlightCrewLinkLineNoService flightCrewLinkLineNoService;
	@Autowired
	private TaskLoopFlightFactService taskLoopFlightFactService;

	@Override
	public String getFlightCom(List<FlightInfo> list) {
		if(CollectionUtils.isEmpty(list)){
			return "";
		}
		StringBuffer sb = new StringBuffer();
		Set<String> flightNoSet = new HashSet<>();
		for(FlightInfo f: list){
			String flightNo = f.getFlightNo().replaceAll("ABC", "");
			int size1 = flightNoSet.size();
			flightNoSet.add(flightNo);
			int size2 = flightNoSet.size();
			
			if(size2>size1){
				sb.append("/"+flightNo);
			}	
		}
			
		return sb.substring(1).toString();
	}

	@Override
	public int getFlightDuration(FlightInfo f) {
		int flightDuration  = 0;
		///任务书中的空中时间>ACARS滑入-滑出>QAR滑入-滑出
		Integer airDurationCfp = f.getAirDurationCfp();
		Date outTimeAcars = f.getOutTimeAcars();
		Date innTimeAcars = f.getInnTimeAcars();
		
		if(airDurationCfp !=null && airDurationCfp !=0){
			flightDuration =  airDurationCfp;
		}else if(outTimeAcars !=null && innTimeAcars !=null){
			DateTimeRange dtr = new DateTimeRange(outTimeAcars, innTimeAcars);
			flightDuration = dtr.getDuration();
		}else if(f.getOutTimeQar() !=null && f.getInnTimeQar() !=null){
			//QAR
			DateTimeRange dtr = new DateTimeRange(f.getOutTimeQar(), f.getInnTimeQar());
			flightDuration = dtr.getDuration();
		}else{
			DateTimeRange dtr = new DateTimeRange(f.getStd(), f.getSta());
			flightDuration = dtr.getDuration();

		}
		return flightDuration;
	}

	@Override
	public int getFlightDuration(List<FlightInfo> list) {
		int flightDurationSum  = 0;
		for(FlightInfo f : list){
			flightDurationSum += this.getFlightDuration(f);
		}
		return flightDurationSum;
	}

	@Transactional(transactionManager=PrimaryDataSourceConfig.TRANSACTION_MANAGER)
	@Override
	public int updateCrewLinkLineNo(String dutyCode, Integer flightId,Integer crewLinkLineNo) {	
		Integer crewLinklineNoF = null;
		Integer crewLinklineNoC = null;
		Integer crewLinklineNoA = null;
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_F)){
			crewLinklineNoF = crewLinkLineNo;
		}
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_C)){
			crewLinklineNoC = crewLinkLineNo;
		}
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_A)){
			crewLinklineNoA = crewLinkLineNo;
		}
		
		//更新T2001的连线号
		int update = flightInfoMapper.updateCrewLinkLineNo(flightId, crewLinklineNoF, crewLinklineNoC, crewLinklineNoA);
		
		//更新航班连线关系表
		FlightInfo flightInfo = flightInfoMapper.selectByPrimaryKey(flightId);
		FlightCrewLinkLineNo fcn = flightCrewLinkLineNoService.getFlightCrewLinkLineNo(flightId, dutyCode);
		if(fcn == null){
			fcn = new FlightCrewLinkLineNo();
			fcn.setFlightId(flightId);
			fcn.setFlightDate(flightInfo.getFlightDate());
			fcn.setCrewLinkLine(crewLinkLineNo);
			fcn.setModuleFlag(dutyCode);
			flightCrewLinkLineNoService.saveSelective(fcn);
			
		}else{
			fcn.setFlightId(flightId);
			fcn.setFlightDate(flightInfo.getFlightDate());
			fcn.setCrewLinkLine(crewLinkLineNo);
			fcn.setModuleFlag(dutyCode);
			flightCrewLinkLineNoService.updateByPrimaryKeySelective(fcn);
		}
		
		return update;
	}

	@Override
	public int updateTaskLoopFact(String dutyCode, int flightId, String taskLoopFact) {
		FlightInfo flightInfo = flightInfoMapper.selectByPrimaryKey(flightId);
		String oldTaskLoopFact = "";
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_F)){
			oldTaskLoopFact =  flightInfo.getTaskLoopFactF();
		}
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_C)){
			oldTaskLoopFact =  flightInfo.getTaskLoopFactC();
		}
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_A)){
			oldTaskLoopFact =  flightInfo.getTaskLoopFactA();
		}
		
		if(StringUtils.isBlank(taskLoopFact) && StringUtils.isBlank(oldTaskLoopFact) ){
			List<TaskLoopFlightFact> loopList = taskLoopFlightFactService.listTaskLoopFlightFactByFlightId(dutyCode, flightId);
			Set<String> set = taskLoopFlightFactService.getTaskLoopFactSet(loopList);
			if(set.size() == 1){
				taskLoopFact = loopList.get(0).getTaskLoopFact();
			}else{
				taskLoopFact = taskLoopFlightFactService.buildTaskLoopFact();
			}
		}
		
//		if(useOldTaskLoopFact){
//			taskLoopFact = oldTaskLoopFact;
//		}
		
		return taskLoopFlightFactService.saveOrUpdateTaskLoopFact( dutyCode, flightInfo.getFlightDate() ,flightId,  taskLoopFact);
	}

	@Override
	public boolean isNightFly(FlightInfo flightInfo) {
//		DateTime std = new DateTime(flightInfo.getStd());
//	
//		
//		int stdHour = std.getHourOfDay();
//		if(stdHour>=0 && stdHour<=4){
//			return true;
//		}
		
		DateTime sta =  new DateTime(flightInfo.getSta());
		int staHour = sta.getHourOfDay();
		if(staHour>=0 && staHour<=4){
			return true;
		}
		
		return false;
	}

	@Override
	public boolean isBadRouteFlight(FlightInfo flightInfo,String dutyCode) {
		List<TaskLoopFlightFact> list = flightInfo.getTaskLoopFlightFactList();
		boolean isBadRouteFlight = false;
		for(TaskLoopFlightFact tlff : list){
			if(StringUtils.equals(tlff.getModuleFlag(),dutyCode)
					&& tlff.getBadRoute() !=null && tlff.getBadRoute() == true){
				isBadRouteFlight = true;
			}
		}
		return isBadRouteFlight;
	}

	@Override
	public List<FlightInfo> getFlightInfoList(List<String> flightIds) {
		return flightInfoMapper.batchSelectInfo(flightIds);
	}

	@Override
	public List<FlightInfo> getFlightInfoByWorkcode(List<FlightInfo> flightInfoList, String workcode) {
		List<FlightInfo> resultList = new ArrayList<>();
		if(flightInfoList != null && flightInfoList.size()>0 && workcode!=null && (!"".equals(workcode))){
			for (FlightInfo info:flightInfoList){
				if(info.getCrewWorkcodes().contains(workcode)){
					resultList.add(info);
				}
			}
		}
		return resultList;
	}

	@Override
	public int getCrewLinkLineNo(FlightInfo flightInfo, String dutyCode) {
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_F)){
			return flightInfo.getCrewLinklineNoF() == null ? 0 : flightInfo.getCrewLinklineNoF();
		}else if(StringUtils.equals(dutyCode, CrewConstant.DUTY_C)){
			return flightInfo.getCrewLinklineNoC() == null ? 0 : flightInfo.getCrewLinklineNoC();
		}else if(StringUtils.equals(dutyCode, CrewConstant.DUTY_A)){
			return flightInfo.getCrewLinklineNoA() == null ? 0 : flightInfo.getCrewLinklineNoA();
		}else{
			return 0;
		}
		
	}

	@Override
	public String getTaskLoopFact(FlightInfo flightInfo, String dutyCode) {
		if(StringUtils.equals(dutyCode, CrewConstant.DUTY_F)){
			return flightInfo.getTaskLoopFactF();
		}else if(StringUtils.equals(dutyCode, CrewConstant.DUTY_C)){
			return flightInfo.getTaskLoopFactC();
		}else if(StringUtils.equals(dutyCode, CrewConstant.DUTY_A)){
			return flightInfo.getTaskLoopFactA();
		}else{
			return "";
		}
		
	}

	@Override
	public Date getLastFlightDate(FlightInfo flightInfo) {
		return flightInfoMapper.getLastFlightDate(flightInfo);
	}

}
